package com.augurit.smartform.service.impl;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.common.util.lang.StringUtils;
import com.augurit.smartform.converter.ComponentRegisterConverter;
import com.augurit.smartform.dao.SmComponentRegisterDao;
import com.augurit.smartform.entity.SmComponentRegister;
import com.augurit.smartform.service.IComponentDefinitionService;
import com.augurit.smartform.service.IComponentRegisterService;
import com.augurit.smartform.util.XMLHelper;
import com.augurit.smartform.web.component.form.ComponentDefinitionForm;
import com.augurit.smartform.web.component.form.ComponentRegisterForm;

@Service
@Transactional
public class ComponentRegisterService implements IComponentRegisterService {
	@Autowired
	private SmComponentRegisterDao componentRegisterDao;
	@Autowired
	private IComponentDefinitionService componentDefinitionService;
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public ComponentRegisterForm get(Long id){
		SmComponentRegister entity=componentRegisterDao.get(id);
		ComponentRegisterForm form=ComponentRegisterConverter.convertToForm(entity);
		return form;
	}
	
	/**
	 * 获取系统所有组件注册目录
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ComponentRegisterForm> getAllComponentRegister(){
		List<SmComponentRegister> list=componentRegisterDao.getAllComponentRegister();
		return ComponentRegisterConverter.convertToFormList(list);
	}
	
	/**
	 * 获取组件注册文件
	 * @return
	 */
	@Transactional(readOnly = true)
	public ComponentRegisterForm getRegistFile(){
		SmComponentRegister entity=componentRegisterDao.getRegisterFile();
		return ComponentRegisterConverter.convertToForm(entity);
	}
	
	/**
	 * 保存并注册组件目录
	 * @param form
	 */
	public void saveAndRegist(ComponentRegisterForm form){
		if(form.getId()!=null){
			ComponentRegisterForm originalForm=this.get(form.getId());
			this.unRegist(originalForm);
			this.regist(form);
			componentRegisterDao.updateByHql(form);
		}else{
			this.regist(form);
			this.save(form);
		}
	}
	
	/**
	 * 注册组件
	 * @param form
	 */
	public void regist(ComponentDefinitionForm form){
		ComponentRegisterForm registForm=this.get(form.getRegisterId());
		this.unRegist(registForm);
		this.regist(registForm);
	}
	
	/**
	 * 注销组件
	 * @param form
	 */
	public void unRegist(ComponentDefinitionForm form){
		ComponentRegisterForm registForm=this.get(form.getRegisterId());
		SmComponentRegister registerFile=componentRegisterDao.getRegisterFile();
		if(registerFile==null){
			registerFile=componentRegisterDao.initRegisterFile();
		}
		String content=registerFile.getContent();
		Document doc=XMLHelper.getDocument(content);
		List<Element> list=doc.selectNodes("/componentlist/group[@cnname='"+registForm.getName()+"']/component/name");
		for(Element element : list){
			if(form.getTag().equals(element.getText())){
				element.getParent().getParent().remove(element.getParent());
				registerFile.setContent(doc.asXML());
				break;
			}
		}
		componentRegisterDao.save(registerFile);
	}
	
	/**
	 * 删除并注销组件
	 * @param id
	 */
	public void deleteAndUnregist(ComponentRegisterForm form){
		this.unRegist(form);
		SmComponentRegister entity=componentRegisterDao.get(form.getId());
		componentRegisterDao.delete(entity);
	}
	
	/**
	 * 禁用并注销组件
	 * @param form
	 */
	public void disable(ComponentRegisterForm form){
		this.unRegist(form);
		componentDefinitionService.disableByRegisterId(form.getId());
		componentRegisterDao.disableByHql(form);
//		SmComponentRegister entity=componentRegisterDao.get(form.getId());
//		entity.setAvailable("0");
//		componentRegisterDao.save(entity);
	}
	
	/**
	 * 启用并注册组件
	 * @param form
	 */
	public void enable(ComponentRegisterForm form){
		SmComponentRegister entity=componentRegisterDao.get(form.getId());
		entity.setAvailable("1");
		componentRegisterDao.save(entity);
		componentDefinitionService.enableByRegisterId(form.getId());
		this.regist(form);
	}
	
	/**
	 * 通过组件目录名称进行查找
	 * @param name
	 * @return
	 */
	@Transactional(readOnly = true)
	public ComponentRegisterForm queryByName(String name){
		SmComponentRegister entity=componentRegisterDao.queryByName(name);
		return ComponentRegisterConverter.convertToForm(entity);
	}

	/**
	 * 注册组件目录
	 * @param form
	 */
	public void regist(ComponentRegisterForm form){
		SmComponentRegister registerFile=componentRegisterDao.getRegisterFile();
		if(registerFile==null){
			registerFile=componentRegisterDao.initRegisterFile();
		}
		String content=registerFile.getContent();
		if(!this.existNode(form, content)){
			Document doc=XMLHelper.getDocument(content);
			Element root=(Element)doc.selectNodes("/componentlist").get(0);
			Element groupEle=XMLHelper.generateDom4jElement("group");
			groupEle.addAttribute("cnname", form.getName());
			root.add(groupEle);
			this.registComponent(form, groupEle);
			
			registerFile.setContent(doc.asXML());
			componentRegisterDao.save(registerFile);
		}
	}
	
	/**
	 * 注册组件目录
	 * @param form
	 */
	public void unRegist(ComponentRegisterForm form){
		SmComponentRegister registerFile=componentRegisterDao.getRegisterFile();
		if(registerFile==null){
			registerFile=componentRegisterDao.initRegisterFile();
		}
		String content=registerFile.getContent();
		if(this.existNode(form, content)){
			Document doc=XMLHelper.getDocument(content);
			List<Element> list=doc.selectNodes("/componentlist/group[@cnname='"+form.getName()+"']");
			Element element=list.get(0);
			element.getParent().remove(element);
			registerFile.setContent(doc.asXML());
		}
		componentRegisterDao.save(registerFile);
	}
	
	/**
	 * 保存表单数据到数据库中
	 * @param form
	 */
	private void save(ComponentRegisterForm form){
		if(form!=null){
			SmComponentRegister entity = null;
			if(form.getId()!=null){
				entity = componentRegisterDao.get(form.getId());
				ComponentRegisterConverter.convertToEntity(form, entity);
			}else{
				entity = new SmComponentRegister();
				form.setType("1");
				form.setAvailable("1");
				ComponentRegisterConverter.convertToEntity(form, entity);
			}
			if(entity!=null){
				componentRegisterDao.save(entity);
			}
		}
	}
	
	/**
	 * 向组件定义文件中注册组件
	 * @param form
	 * @param group
	 */
	private void registComponent(ComponentRegisterForm form,Element group){
		List<ComponentDefinitionForm> list=componentDefinitionService.getDefinitionByRegisterId(form.getId());
		if(list!=null){
			for(ComponentDefinitionForm defForm : list){
				Element componentEle=XMLHelper.generateDom4jElement("component");
				Element nameEle=XMLHelper.generateDom4jElement("name");
				nameEle.addText(defForm.getTag());
				Element cnnameEle=XMLHelper.generateDom4jElement("cnname");
				cnnameEle.addText(defForm.getTagCn());
				Element imgEle=XMLHelper.generateDom4jElement("img");
				imgEle.addText(defForm.getIcon());
				componentEle.add(nameEle);
				componentEle.add(cnnameEle);
				componentEle.add(imgEle);
				group.add(componentEle);
			}
		}
	}
	
	/**
	 * 检查是否已经存在某个节点
	 * @param form
	 * @param content
	 * @return
	 */
	private boolean existNode(ComponentRegisterForm form,String content){
		boolean bol=false;
		if(form!=null&&StringUtils.isNotBlank(form.getName())){
			String groupName=form.getName();
			Document doc=XMLHelper.getDocument(content);
			List<Element> list=doc.selectNodes("/componentlist/group[@cnname='"+groupName+"']");
			if(list!=null&&list.size()>0){
				bol=true;
			}
		}
		return bol;
	}
}
